<?php
// +----------------------------------------------------------------------
// |     【SystemConfig】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Session;
use think\facade\Request;

class SystemConfig extends Model{
    use SoftDelete;
    protected $deleteTime = false;
	
    // 获取列表
    public static function getList($where = []){
        $limit = I('get.limit/d');

        $list = self::order('id','desc')->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	// 获取列表数据
	public static function listData($where=[],$order="",$n_page=0,$limit=20){
		$data=self::order($order)->where($where);
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();
		
		return $data;
	}
	
	// 获取某个单条记录
	public static function info($id){
		if(!is_array($id)){
			$model=self::find($id);
		}else{
			$model=self::where($id)->find();
		}
		return $model;		
	}
	
	// 获取某个参数
	public static function get($name=''){
        if($name==''){
            // 获取数据
            $data=self::select();
            return $data;
        }else{
            $names=explode('.',$name);
            $key=$names[0];
            
            // 获取数据
            $data=unserialize(self::where(['name'=>$key])->value('val'));
            if($data==''){
            	return [];
            }else{
            	switch(count($names)){
                    case 1:
                    	return $data??[];
                    	break;
            		case 2:
            			return $data[$names[1]]??[];
            			break;
            		case 3:
            			return $data[$names[1]][$names[2]]??[];
            			break;
            		case 4:
            			return $data[$names[1]][$names[2]][$names[3]]??[];
            			break;
                    case 5:
                    	return $data[$names[1]][$names[2]][$names[3]][$names[4]]??[];
                    	break;
            		default :
                        return [];
            	}
            }	
        }
	}
	
	
	// 获取器
	public function getValAttr($value){
		return unserialize($value);
	}
}
